/**
 * UserDaoImpl.java
 * 
 * Copyright@2015 OVT Inc. All rights reserved. 
 * 
 * May 4, 2015
 */
package com.ovt.doorbell.payment.dao;

import java.text.MessageFormat;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.ovt.doorbell.payment.common.utils.DataConvertUtils;
import com.ovt.doorbell.payment.dao.mapper.PermissionMapper;
import com.ovt.doorbell.payment.dao.vo.PageInfo;
import com.ovt.doorbell.payment.dao.vo.Permission;

/**
 * UserDaoImpl
 * 
 * @Author zhi.liu
 * @Version 1.0
 * @See
 * @Since [OVT Cloud Platform]/[DAO] 1.0
 */

@Repository
public class PermissionDaoImpl implements PermissionDao
{
    @Autowired
    private DaoHelper daoHelper;
    @Autowired
    private PermissionMapper permissionMapper;
    
    private static final String SQL_GET_PREFIX = "SELECT id,perm_name,perm_desc FROM permission ";
    
    private static final String SQL_GET_PERMISSION_LIST = SQL_GET_PREFIX 
            + "ORDER BY {0} {1} LIMIT {2}, {3}";
    
    private static final String SQL_GET_PERMISSION_BY_USERID = SQL_GET_PREFIX 
            + "WHERE id "
            + "IN (SELECT ap_perm_id FROM admin_permission WHERE ap_admin_id = ?)";
    
    private static final String SQL_GET_UNPERMISSION_BY_USERID = SQL_GET_PREFIX 
            + "WHERE id "
            + "NOT IN (SELECT ap_perm_id FROM admin_permission WHERE ap_admin_id = ?)";
    
    @Override
    public List<Permission> getAllPermission(PageInfo page)
    {
        String sql = MessageFormat.format(
                         SQL_GET_PERMISSION_LIST,
                         page.getSortBy(),
                         page.getOrder().toString(),
                         DataConvertUtils.toString(page.getPageNo() * page.getPageSize()),
                         DataConvertUtils.toString(page.getPageSize()));

        String errMsg = MessageFormat.format(
                            "Failed to query permission list with page info: {0}!",
                            page);
        
        List<Permission> permissions = this.daoHelper.queryForList(sql, permissionMapper, errMsg);
        
        return permissions;
    }
    @Override
    public List<Permission> getPermissionByUserId(int userId)
    {
        String errMsg =
                MessageFormat.format(
                        "Failed to query permission list with user id: {0}!",
                        userId);
        List<Permission> permissions = this.daoHelper.queryForList(SQL_GET_PERMISSION_BY_USERID, permissionMapper, errMsg);
        
        return permissions;
    }

    public List<Permission> getUnPermissionByUserId(int userId)
    {
        String errMsg =
                MessageFormat.format(
                        "Failed to query unpermission list with user id: {0}!",
                        userId);
        List<Permission> permissions = this.daoHelper.queryForList(SQL_GET_UNPERMISSION_BY_USERID, permissionMapper, errMsg);
        
        return permissions;
    }

}